package user

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
	"time"
)

type TanchuangRBody struct {
	Tanchuangimg string `json:"tanchuangimg"`
	Huodongimg   string `json:"huodongimg"`
}

func Tanchuang(ctx *gin.Context) {
	request := UserinfoBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid

	now := time.Now()

	// 设置为今天的0点
	midnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
	//start, end := getWeekStartAndEnd()
	// 转换为Unix时间戳
	midnightTimestamp := midnight.Unix()
	var shandong models.Shandong
	data_source.Db.Where("weekend > ?", time.Now().Unix()).Where("uid = ?", uid).First(&shandong)
	if shandong.ID > 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 400,
			"data":    "on",
			"msg":     "不弹出",
		})
		return
	}
	var taocan models.Tanchuang
	data_source.Db.Where("addtime = ?", midnightTimestamp).Where("uid = ?", uid).First(&taocan)
	if taocan.ID > 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 400,
			"data":    "on",
			"msg":     "不弹出",
		})
		return
	}

	tanchuangadd := models.Tanchuang{
		Uid:     uid,
		Addtime: midnightTimestamp,
	}
	data_source.Db.Create(&tanchuangadd)
	data := TanchuangRBody{
		Tanchuangimg: "https://shanyu.jiayixunfang.cn/upload/tanchuang.png",
		Huodongimg:   "https://shanyu.jiayixunfang.cn/upload/huodong.png",
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

func Shandong(ctx *gin.Context) {
	request := UserinfoBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid

	start, end := getWeekStartAndEnd()

	tanchuangadd := models.Shandong{
		Uid:     uid,
		Sex:     request.Sex,
		Addtime: time.Now().Unix(),
		Weekint: start,
		Weekend: end,
	}
	data_source.Db.Create(&tanchuangadd)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func getWeekStartAndEnd() (startTime, endTime int64) {
	// 获取当前时间
	now := time.Now()

	// 获取本周的第一天（周日）
	startOfWeek := now.AddDate(0, 0, -int(now.Weekday()))
	startOfWeek = time.Date(startOfWeek.Year(), startOfWeek.Month(), startOfWeek.Day(), 0, 0, 0, 0, startOfWeek.Location())
	startTime = startOfWeek.Unix()

	// 本周的最后一天（周六）
	endOfWeek := startOfWeek.AddDate(0, 0, 6)
	endOfWeek = time.Date(endOfWeek.Year(), endOfWeek.Month(), endOfWeek.Day(), 0, 0, 0, 0, endOfWeek.Location())
	endTime = endOfWeek.Unix()

	return
}

// GetUser 拉取单个报名对象
func GetUser(ctx *gin.Context) {
	timeNow := time.Now()
	if timeNow.Weekday() != time.Friday {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    -1,
			"error":   0,
			"success": 200,
			//"data":    ans,
			"msg": "活动未开始",
		})
		return
	}
	var start, end, duration models.Config
	data_source.Db.Where("id = 10").First(&start)
	data_source.Db.Where("id = 11").First(&end)
	data_source.Db.Where("id = 12").First(&duration)

	startTime := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), int(start.Value), 0, 0, 0, timeNow.Location())
	endTime := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), int(end.Value), 0, 0, 0, timeNow.Location())

	//活动还未开始
	if timeNow.Before(startTime) {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    -1,
			"error":   0,
			"success": 200,
			//"data":    ans,
			"msg": "活动未开始",
		})
		return
	}
	// 活动已结束
	if timeNow.After(endTime) {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    -2,
			"error":   0,
			"success": 200,
			//"data":    ans,
			"msg": "活动已结束",
		})
		return
	}
	index := int64(timeNow.Sub(startTime).Minutes()) / duration.Value

	request := UserinfoBody{}
	ctx.ShouldBind(&request)

	startInt, endInt := getWeekStartAndEnd()

	targetSex := 1
	if request.Sex == 1 {
		targetSex = 2
	}

	var shandong models.Shandong
	data_source.Db.
		Where("sex = ?", targetSex).
		Where("addtime between ? and ?", startInt, endInt).
		Order("id").
		Offset(int(index)).
		First(&shandong)
	// 已经推送完成，返回总共有多少相互喜欢的
	if shandong.ID == 0 {
		var count int64
		data_source.Db.
			Where("user_id in (select uid from user_like where state = 1) and state = 1").
			Where("add_time between ? and ?", startInt, endInt).
			Count(&count)
		ctx.JSON(http.StatusOK, gin.H{
			"code":    -3,
			"error":   0,
			"success": 200,
			"data":    count,
			//"msg":     "活动已结束",
		})
		return
	}
	var ans models.User
	data_source.Db.Where("id = ?", shandong.Uid).First(&ans)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    ans,
		"msg":     "ok",
	})
}

func GetLikeEachOther(ctx *gin.Context) {
	request := UserinfoBody{}
	ctx.ShouldBindQuery(&request)

	uid := 21530

	var like models.Userlike
	data_source.Db.Where("userid in (select uid from userlike where userid = ? and state = 1 and pushed = 0) and uid = ? and state = 1 and pushed = 0", uid, uid).First(&like)
	if like.ID == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"msg":     "ok",
		})
		return
	}

	if err := data_source.Db.Model(&models.Userlike{}).Where("id = ?", like.ID).Update("pushed", 1).Error; err != nil {
		fmt.Println(err)
	}

	var ans models.User
	data_source.Db.Where("id = ?", like.Userid).First(&ans)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    ans,
		"msg":     "ok",
	})
}
